//
//  PhotoModel.m
//  WOFishingBoat
//
//  Created by 郑逸凯 on 2019/1/7.
//  Copyright © 2019年 郑逸凯. All rights reserved.
//

#import "PhotoModel.h"
#import <YASDK/YADateFormat.h>

@implementation PhotoModel

+ (instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary {
    PhotoModel *model = [super yy_modelWithDictionary:dictionary];
    model.isSelected = NO;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    model.sectionTime =  [formatter stringFromDate:model.time];
    model.mwPhoto = [MWPhoto photoWithURL:[NSURL URLWithString:model.snapShot]];
    //生成原图路径
    model.mwBigPhoto = [MWPhoto photoWithURL:[NSURL URLWithString:[self originalPathWiththumbnailPath:model.snapShot]]];
    if ([model.kind isEqualToString:@"video"]) {
        model.mwBigPhoto.isVideo = YES;
        model.mwBigPhoto.videoURL = [NSURL URLWithString:model.videoSrc];
//#warning test
//        model.mwBigPhoto.videoURL = [NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"];
        model.isVideo = YES;
    }
    return model;
}

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
             @"boatID" : @"id",
             };
}

- (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
    NSNumber *timestamp = dic[@"time"];
    if (![timestamp isKindOfClass:[NSNumber class]]) return NO;
    _time = [NSDate dateWithTimeIntervalSince1970:timestamp.floatValue];
    NSNumber *duration = dic[@"duration"];
    if (![duration isKindOfClass:[NSNumber class]]) return NO;
    _duration = [YADateFormat getHHMMSSAutoHideHHFromSS:[NSString stringWithFormat:@"%@",duration]];
    return YES;
}

+ (NSString *)originalPathWiththumbnailPath:(NSString *)thumbnailPath {
    NSString *lastPathComponent = [thumbnailPath lastPathComponent];
    NSString *newLastPathComponent = [lastPathComponent stringByReplacingOccurrencesOfString:@"-small" withString:@""];
    NSString *originalPath = [thumbnailPath stringByReplacingOccurrencesOfString:lastPathComponent withString:newLastPathComponent];
    return originalPath;
}

- (NSString *)imageName {
    NSString *lastPathComponent = [self.snapShot lastPathComponent];
    NSString *newLastPathComponent = [lastPathComponent stringByReplacingOccurrencesOfString:@"-small" withString:@""];
    return newLastPathComponent;
}

@end
